﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ultima;
using SpellCaster3.Core.Drawing;

namespace SpellCaster3.Forms.Controls
{
    public partial class GumpControl : UserControl
    {
        protected UltimaIconBitmap UltimaImage { get; private set; }
        private bool _Highlighted = false;

        public GumpControl() : this(-1)
        {
        }

        public GumpControl(int gumpIndex)
        {
            InitializeComponent();
            UltimaImage = new UltimaIconBitmap() { AutoRender = false };

            GumpIndex = gumpIndex;
        }

        public virtual void Reset()
        {
            GumpIndex = GumpPicker.BaseRange.Minimum;
            HueIndex = 1;
            Highlighted = false;
            Enabled = true;
            Render();
        }

        public int GumpIndex
        {
            get { return UltimaImage.GumpIndex; }
            set
            {
                Visible = value >= 0;
                UltimaImage.GumpIndex = value;
                Render();
            }
        }

        public int HueIndex
        {
            get { return UltimaImage.HueIndex; }
            set
            {
                UltimaImage.HueIndex = value;
                Render();
            }
        }

        protected string GumpText
        {
            get { return gumpIndex.Text; }
            set
            {
                gumpIndex.Text = value;
            }
        }

        protected Image Image
        {
            get { return gumpPictureBox.Image; }
            set
            {
                gumpPictureBox.Image = value;
            }
        }

        protected virtual void Render()
        {
            GumpText = UltimaImage.GumpIndex.ToString();
            UltimaImage.Refresh();
            Image = UltimaImage.Image;
        }

        public bool Highlighted
        {
            get { return _Highlighted; }
            private set
            {
                if (value != _Highlighted)
                {
                    _Highlighted = value;
                    BackColor = (value ? Color.Green : Color.White);
                }
            }
        }

        private void GumpControl_Click(object sender, EventArgs e)
        {
            //Al click su una parte non dell'usercontrol (l'immagine o la label), triggera un click sul control (in modo che sia trackabile)
            OnClick(e);
        }

        private void GumpControl_MouseEnter(object sender, EventArgs e)
        {
            if (Enabled) Highlighted = true;
        }

        private void GumpControl_MouseLeave(object sender, EventArgs e)
        {
            if (Enabled) Highlighted = false;
        }

        private void GumpControl_EnabledChanged(object sender, EventArgs e)
        {
            UltimaImage.Enabled = Enabled;
            Highlighted = Enabled;
            BackColor = (Enabled ? Color.White : Color.FromKnownColor(KnownColor.Control));
            Render();
        }
    }
}
